package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class ExpandableRecyclerViewSlice extends AbilitySlice {

    private Image back_button, imgStrawberry, imgApple, imgOrange, imgLemon, imgBeer, imgLime, imgWatermelon, imgBlueberry, imgPlum;
    private Text text_title;
    private DependentLayout ddlStrawberry, ddlApple, ddlOrange, ddlLemon, ddlBeer, ddlLime, ddlWatermelon, ddlBlueberry, ddlPlum;
    private boolean isStrawberry, disApple, isOrange, isLemon, isBeer, isLime, isWatermelon, isBlueberry, isPlum;
    private Component inStrawberry, inApple, inOrange, inLemon, inBeer, inLime, inWatermelon, inBlueberry, inPlum;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_expandable_recycler_view);
        initView();
        initClick();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0){
            if (MainAbilitySlice.all.containsKey("ExpandableRecyclerView")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("ExpandableRecyclerView")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("ExpandableRecyclerView", integer);
                        pf.flushSync();
                    }
                }
            }else {
                pf.putInt("ExpandableRecyclerView", 1);
                pf.flushSync();
            }
        }else {
            pf.putInt("ExpandableRecyclerView", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("ExpandableRecyclerView");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));

        //一
        ddlStrawberry = (DependentLayout) findComponentById(ResourceTable.Id_ddl_strawberry);
        imgStrawberry = (Image) findComponentById(ResourceTable.Id_img_strawberry);
        inStrawberry = findComponentById(ResourceTable.Id_in_strawberry);
        //二
        ddlApple = (DependentLayout) findComponentById(ResourceTable.Id_ddl_apple);
        imgApple = (Image) findComponentById(ResourceTable.Id_img_apple);
        inApple = findComponentById(ResourceTable.Id_in_apple);
        //三
        ddlOrange = (DependentLayout) findComponentById(ResourceTable.Id_ddl_orange);
        imgOrange = (Image) findComponentById(ResourceTable.Id_img_orange);
        inOrange = findComponentById(ResourceTable.Id_in_orange);
        //四
        ddlLemon = (DependentLayout) findComponentById(ResourceTable.Id_ddl_lemon);
        imgLemon = (Image) findComponentById(ResourceTable.Id_img_lemon);
        inLemon = findComponentById(ResourceTable.Id_in_lemon);
        //五
        ddlBeer = (DependentLayout) findComponentById(ResourceTable.Id_ddl_beer);
        imgBeer = (Image) findComponentById(ResourceTable.Id_img_beer);
        inBeer = findComponentById(ResourceTable.Id_in_beer);
        //六
        ddlLime = (DependentLayout) findComponentById(ResourceTable.Id_ddl_lime);
        imgLime = (Image) findComponentById(ResourceTable.Id_img_lime);
        inLime = findComponentById(ResourceTable.Id_in_lime);
        //七
        ddlWatermelon = (DependentLayout) findComponentById(ResourceTable.Id_ddl_watermelon);
        imgWatermelon = (Image) findComponentById(ResourceTable.Id_img_watermelon);
        inWatermelon = findComponentById(ResourceTable.Id_in_watermelon);
        //八
        ddlBlueberry = (DependentLayout) findComponentById(ResourceTable.Id_ddl_blueberry);
        imgBlueberry = (Image) findComponentById(ResourceTable.Id_img_blueberry);
        inBlueberry = findComponentById(ResourceTable.Id_in_blueberry);
        //九
        ddlPlum = (DependentLayout) findComponentById(ResourceTable.Id_ddl_plum);
        imgPlum = (Image) findComponentById(ResourceTable.Id_img_plum);
        inPlum = findComponentById(ResourceTable.Id_in_plum);
        back_button.setClickedListener(component -> onBackPressed());
    }

    private void initClick() {
        ddlStrawberry.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = imgStrawberry.createAnimatorProperty();
                if (isStrawberry == false) {
                    inStrawberry.setVisibility(Component.VISIBLE);
                    animator.rotate(180).setDuration(300);
                    isStrawberry = true;
                } else {
                    inStrawberry.setVisibility(Component.HIDE);
                    animator.rotate(0).setDuration(300);
                    isStrawberry = false;
                }
                animator.start();
            }
        });
        ddlApple.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = imgApple.createAnimatorProperty();
                if (disApple == false) {
                    inApple.setVisibility(Component.VISIBLE);
                    animator.rotate(180).setDuration(300);
                    disApple = true;
                } else {
                    inApple.setVisibility(Component.HIDE);
                    animator.rotate(0).setDuration(300);
                    disApple = false;
                }
                animator.start();
            }
        });
        ddlOrange.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = imgOrange.createAnimatorProperty();
                if (isOrange == false) {
                    inOrange.setVisibility(Component.VISIBLE);
                    animator.rotate(180).setDuration(300);
                    isOrange = true;
                } else {
                    inOrange.setVisibility(Component.HIDE);
                    animator.rotate(0).setDuration(300);
                    isOrange = false;
                }
                animator.start();
            }
        });
        ddlLemon.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = imgLemon.createAnimatorProperty();
                if (isLemon == false) {
                    inLemon.setVisibility(Component.VISIBLE);
                    animator.rotate(180).setDuration(300);
                    isLemon = true;
                } else {
                    inLemon.setVisibility(Component.HIDE);
                    animator.rotate(0).setDuration(300);
                    isLemon = false;
                }
                animator.start();
            }
        });
        ddlBeer.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = imgBeer.createAnimatorProperty();
                if (isBeer == false) {
                    inBeer.setVisibility(Component.VISIBLE);
                    animator.rotate(180).setDuration(300);
                    isBeer = true;
                } else {
                    inBeer.setVisibility(Component.HIDE);
                    animator.rotate(0).setDuration(300);
                    isBeer = false;
                }
                animator.start();
            }
        });
        ddlLime.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = imgLime.createAnimatorProperty();
                if (isLime == false) {
                    inLime.setVisibility(Component.VISIBLE);
                    animator.rotate(180).setDuration(300);
                    isLime = true;
                } else {
                    inLime.setVisibility(Component.HIDE);
                    animator.rotate(0).setDuration(300);
                    isLime = false;
                }
                animator.start();
            }
        });
        ddlWatermelon.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = imgWatermelon.createAnimatorProperty();
                if (isWatermelon == false) {
                    inWatermelon.setVisibility(Component.VISIBLE);
                    animator.rotate(180).setDuration(300);
                    isWatermelon = true;
                } else {
                    inWatermelon.setVisibility(Component.HIDE);
                    animator.rotate(0).setDuration(300);
                    isWatermelon = false;
                }
                animator.start();
            }
        });
        ddlBlueberry.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = imgBlueberry.createAnimatorProperty();
                if (isBlueberry == false) {
                    inBlueberry.setVisibility(Component.VISIBLE);
                    animator.rotate(180).setDuration(300);
                    isBlueberry = true;
                } else {
                    inBlueberry.setVisibility(Component.HIDE);
                    animator.rotate(0).setDuration(300);
                    isBlueberry = false;
                }
                animator.start();
            }
        });
        ddlPlum.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = imgPlum.createAnimatorProperty();
                if (isPlum == false) {
                    inPlum.setVisibility(Component.VISIBLE);
                    animator.rotate(180).setDuration(300);
                    isPlum = true;
                } else {
                    inPlum.setVisibility(Component.HIDE);
                    animator.rotate(0).setDuration(300);
                    isPlum = false;
                }
                animator.start();
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
